package com.zhengyisky.controller;

import com.zhengyisky.entity.*;
import com.zhengyisky.service.NewsSerivce;
import com.zhengyisky.utils.OSSFileUtil;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/news")
public class NewsController {

    @Autowired
    private NewsSerivce newsSerivce;

    @Autowired
    private OSSFileUtil ossFileUtil;

    @GetMapping("/list")
    public Result list(QueryParam queryParam){
        //List<News> list = newsSerivce.list();

        System.out.println("测试合并分支2222");


        Map map = newsSerivce.listPage(queryParam);
        return Result.success(map);
    }

    @GetMapping("/typeList")
    public Result typeList(){
        List<Type> list = newsSerivce.typeList();
        return Result.success(list);
    }

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {
        String path = ossFileUtil.uploadFile(file);
        return Result.success(path);
    }

    @PostMapping("/save")
    public Result save(@RequestBody AddEntity addEntity) {
        newsSerivce.save(addEntity);
        return Result.success();
    }

    //restful   get post   put  delete

    @PutMapping("/pass")
    public Result pass(Integer id) throws TemplateException, IOException {
        newsSerivce.pass(id);
        return Result.success();
    }

    @PutMapping("/notPass")
    public Result notPass(Integer id, String reason) throws TemplateException, IOException {
        newsSerivce.notPass(id, reason);
        return Result.success();
    }

}
